﻿/**
 * SlideShow Component Main Class - Created by FlashFrameLess Team JAN/2009
 *
 * @author		FabioTNT / FlashFrameLessTeam - http://www.flashframeless.com/
 * @version		1.0
 */

package com.flashframeless.components.ui.slideshow {

	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import caurina.transitions.Tweener;
	
	public class SlideShow extends Sprite {
		
		private var i				: uint;
		private var _prevImage		: Sprite;
		private var _activeImage	: uint;
		private var _timer		 	: Timer;
		private var _transitionTime	: uint			= 5000;
		private var _arrImages		: Array 		= new Array();
		
		public function SlideShow():void {			
			
			instanciate();
			
			setupInteractions();
			
			dispatchEvent( new Event( Event.COMPLETE ) );
			
		}
		
		private function instanciate():void {
			
			//
			
		}
		
		private function setupInteractions():void {
			
			//Init MovieClip Variables
			//initClips();
			
			//Active Listeners
			//activeListeners();
			
		}
		
		/**
		 * Active/Desactive SlideShow.
		 *
		 * @param	booVar	Boolean
		 * @return			void
		 */
		private function activeSlideShow( booVar:Boolean = true ):void{
			
			if ( booVar ) {
				
				_timer = new Timer( _transitionTime );
				_timer.addEventListener( TimerEvent.TIMER, timerHandler );
				
				_timer.start();
				
			} else if( _timer ) {
				
				_timer = null;
				_timer.removeEventListener( TimerEvent.TIMER, timerHandler );
				
				_timer.reset();
				_timer.stop();
				
			}			
			
		}
		
		private function tradeImage( id:uint ):void {			
			
			Tweener.addTween( _prevImage, { alpha:0, time:1, transition:"linear", onComplete:function() {
				
				_prevImage.visible = false;
				
			} } );
			
			_arrImages[id].clip.visible = true;
			
			Tweener.addTween( _arrImages[id].clip, { alpha:1, time:1, transition:"linear" } );
			
		}
		
		/**
		 * Dispose Method.
		 *
		 * @return			void
		 */
		public function dispose():void {
			
			activeSlideShow( false );
			
		}
		
		/**
		 * Play Method.
		 *
		 * @return			void
		 */
		public function play():void {
			
			activeSlideShow();
			
		}
		
		/**
		 * Stop Method.
		 *
		 * @return			void
		 */
		public function stop():void {
			
			activeSlideShow( false );
			
		}
		
		/*-------------------------------------------------------------------------------------------------*\
		|* Handlers ---------------------------------------------------------------------------------------*|
		\*-------------------------------------------------------------------------------------------------*/
		
		/**
		 * Timer Handler.
		 *
		 * @param	evt		TimerEvent
		 * @return			void
		 */
		private function timerHandler( evt:TimerEvent ):void {
			
			_prevImage = _arrImages[ _activeImage ].clip;
			
			if ( _activeImage < _arrImages.length -1 ) {
				
				_activeImage++;
				
			} else {
				
				_activeImage = 0;
				
			}
			
			tradeImage( _activeImage );
			
		}
		
		/*-------------------------------------------------------------------------------------------------*\
		|* Getters e Setters ------------------------------------------------------------------------------*|
		\*-------------------------------------------------------------------------------------------------*/
				
		/**
		 * DataProvider Setter.
		 *
		 * @param	arrImag	Array
		 * @return			void
		 */
		 public function set imageProvider( arrImag:Array ):void {
			
			_arrImages = arrImag;
			
			for ( i = 1; i < _arrImages.length; i++ ) {
				
				_arrImages[i].clip.alpha 	= 0;
				_arrImages[i].clip.visible 	= false;
				
			}
			
		}
		
		/**
		 * Transition Time Setter.
		 *
		 * @param	time	int
		 * @return			void
		 */
		 public function set transitionTime( time:int ):void {
			
			_transitionTime = time;
			
		}	
		
	}
	
}